#include "TLine.h"
#include "TCanvas.h"
#include <fstream>
#include <iostream>
#include <stdlib.h>
using namespace std;

double** ReadMap(char*, int&);
double** ADCMapAdd(double**, int&, double, double, int, int);
void DrawSquare(double, double, double, double, int, int);
double max(double, double);
double min(double, double);

int main(int argc, char** argv) {
	int numberOfCrystals = 0;
	double** crystalInfo = ReadMap(argv[1], numberOfCrystals);

	int newADCNum = crystalInfo[numberOfCrystals - 1][2] + 1;

	for (int i = 0; i < numberOfCrystals; i++) {
		int ADCNum = crystalInfo[i][2];
		bool con = false;
		int j;
		for (j = 0; i + j < numberOfCrystals && crystalInfo[i + j][2] == ADCNum; j++) {
			if (crystalInfo[i + j][1] < 0) {
				con = true;
			}
		}
		if (con) {
			i = i + j - 1;
			continue;
		}
		cout << newADCNum;
		for (j = 0; i + j < numberOfCrystals && crystalInfo[i + j][2] == ADCNum; j++) {
			int crystalNum;
			int I;
			int J;
			if (crystalInfo[i + j][3] > 3000) {
				I = (crystalInfo[i + j][3] - 3000) / 36;
				J = ((int) crystalInfo[i + j][3] - 3000) % 36;
				J = 35 - J;
				crystalNum = 3000 + 36 * I + J;
			} else {
				I = crystalInfo[i + j][3] / 48;
				J = ((int) crystalInfo[i + j][3]) % 48;
				J = 47 - J;
				crystalNum = 48 * I + J;
			}
			cout << "\t" << crystalNum;
		}
		i = i + j - 1;
		cout << "\n";
		newADCNum++;
	}
}

double** ADCMapAdd(double** map, int& count, double x, double y, int ADCNumber,
		int number) {
	double** newMap = new double*[count + 1];

	for (int i = 0; i < count; i++)
		newMap[i] = map[i];
	delete map;

	newMap[count] = new double[4];

	newMap[count][0] = x;
	newMap[count][1] = y;
	newMap[count][2] = ADCNumber;
	newMap[count][3] = number;

	count++;

	return newMap;
}

double** ReadMap(char* filePath, int& totalNum) {
	int ADCMapCount = 0;
	double** ADCMap = new double*[ADCMapCount];

	std::ifstream file;
	file.open(filePath);

	char* number = new char[1024];
	for (int j = 0; j < 1024; j++)
		number[j] = ' ';
	int numberCount = 0;

	bool ADCFlag = true;
	int ADCNumber = 0;

	while (file.good()) {
		char c = file.get();
		if (c == '\t') {
			if (ADCFlag) {
				ADCNumber = atoi(number);
				ADCFlag = false;
			} else {
				int num = atoi(number);
				int i;
				int j;
				if (num < 3000) {
					j = num % 48;
					i = (num - j) / 48;
					ADCMap = ADCMapAdd(ADCMap, ADCMapCount,
							(i + .5 - 24) * 2.5, (j + .5 - 24) * 2.5,
							ADCNumber, num);
				} else {
					j = (num - 3000) % 36;
					i = (num - 3000 - j) / 36;
					ADCMap = ADCMapAdd(ADCMap, ADCMapCount, (i + .5 - 18) * 5,
							(j + .5 - 18) * 5, ADCNumber, num);
				}
			}
			numberCount = 0;
			for (int j = 0; j < 1024; j++)
				number[j] = ' ';
			continue;
		}
		if (c == '\n' || c == EOF) {
			if (!ADCFlag) {
				int num = atoi(number);
				int i;
				int j;
				if (num < 3000) {
					j = num % 48;
					i = (num - j) / 48;
					ADCMap = ADCMapAdd(ADCMap, ADCMapCount,
							(i + .5 - 24) * 2.5, (j + .5 - 24) * 2.5,
							ADCNumber, num);
				} else {
					j = (num - 3000) % 36;
					i = (num - 3000 - j) / 36;
					ADCMap = ADCMapAdd(ADCMap, ADCMapCount, (i + .5 - 18) * 5,
							(j + .5 - 18) * 5, ADCNumber, num);
				}
			}
			ADCFlag = true;
			numberCount = 0;
			for (int j = 0; j < 1024; j++)
				number[j] = ' ';
			continue;
		}

		number[numberCount] = c;
		numberCount++;
	}

	delete number;

	totalNum = ADCMapCount;
	file.close();
	return ADCMap;
}
